/**
 *
 */
package de.cbf.cam.dsa.person.views;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;

import de.cbf.cam.dsa.person.ActivatorPersonDSA;
import de.cbf.cam.dsa.person.base.CompositePersonBaseDSA;
import de.cbf.cam.dsa.person.base.ControllerPersonBaseDSA;

/**
 * class initializes the controllers for the composites
 * @author Alti
 */
public class InitializeControllersListener implements IPartListener2 {
	private final Log logger = LogFactory.getLog(InitializeControllersListener.class);
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener2#partActivated(org.eclipse.ui.IWorkbenchPartReference)
	 */
	@Override
	public void partActivated(IWorkbenchPartReference partRef) {
		logger.debug("part activated: "+partRef);
		IWorkbenchPart part = partRef.getPart(false);
		if (part instanceof PersonView) {
			PersonView personView = (PersonView) part;
			initControllersForPersonView(personView);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener2#partBroughtToTop(org.eclipse.ui.IWorkbenchPartReference)
	 */
	@Override
	public void partBroughtToTop(IWorkbenchPartReference partRef) {
		logger.debug("part brought to top: "+partRef);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
	 */
	@Override
	public void partClosed(IWorkbenchPartReference partRef) {
		logger.debug("part closed: "+partRef);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener2#partDeactivated(org.eclipse.ui.IWorkbenchPartReference)
	 */
	@Override
	public void partDeactivated(IWorkbenchPartReference partRef) {
		logger.debug("part deActivated: "+partRef);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
	 */
	@Override
	public void partOpened(IWorkbenchPartReference partRef) {
		logger.debug("part opened: "+partRef);

	}



	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener2#partHidden(org.eclipse.ui.IWorkbenchPartReference)
	 */
	@Override
	public void partHidden(IWorkbenchPartReference partRef) {
		logger.debug("part hidden: "+partRef);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener2#partVisible(org.eclipse.ui.IWorkbenchPartReference)
	 */
	@Override
	public void partVisible(IWorkbenchPartReference partRef) {
		logger.debug("part visible: "+partRef);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IPartListener2#partInputChanged(org.eclipse.ui.IWorkbenchPartReference)
	 */
	@Override
	public void partInputChanged(IWorkbenchPartReference partRef) {
		logger.debug("part inputChanged: "+partRef);

	}


	/**
	 * @param personView
	 */
	protected void initControllersForPersonView(PersonView personView) {
		ActivatorPersonDSA viewMainController = ActivatorPersonDSA.getDefault();
		CompositePersonBaseDSA cmpPersonBase = personView.getCompositePersonBase();
		initializeControllerPersonBase(viewMainController,cmpPersonBase);

	}

	/**
	 * @param viewMainController
	 * @param cmpPersonBase
	 */
	protected void initializeControllerPersonBase(ActivatorPersonDSA viewMainController,CompositePersonBaseDSA cmpPersonBase) {
		if(cmpPersonBase == null){
			logger.error("compositePersonBase not correctly initialized, cannot initialize controller");
			return;
		}
		ControllerPersonBaseDSA controllerPersonBase = new ControllerPersonBaseDSA();
		controllerPersonBase.setComposite(cmpPersonBase);
		controllerPersonBase.setPerson(viewMainController.getActivePerson());
		controllerPersonBase.setProviderMonths(viewMainController.getProviderMonths());
		controllerPersonBase.setRCPFinder(viewMainController.getRCPFinder());
		viewMainController.setControllerPersonBase(controllerPersonBase);
	}
}
